package com.java.comprehensive.arithmetic.leecode.medium;

import util.ListNode;

public class LeetCode_142 {

    public ListNode detectCycle(ListNode head) {
        ListNode low = head;
        ListNode fast = head;
        while (fast != null && fast.next != null) {
            low = low.next;
            fast = fast.next.next;
            if (low == fast) {
                ListNode index_1 = fast;
                ListNode index_2 = head;
                while (index_2 != index_1) {
                    index_1 = index_1.next;
                    index_2 = index_2.next;
                }
                return index_2;
            }
        }
        return null;
    }

    public static void main(String[] args) {

    }
}
